package com.wltt.product.ahuthr.component;

import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.constants.YesNoConstant;
import com.wltt.product.ahuthr.enums.AgreeSalaryEnum;
import com.wltt.product.ahuthr.enums.SalaryStatusEnum;
import com.wltt.product.ahuthr.enums.SalaryStopTypeEnum;
import com.wltt.product.ahuthr.mapper.*;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.po.*;
import com.wltt.product.ahuthr.model.vo.UpdateSalaryReq;
import com.wltt.product.ahuthr.model.vo.base.*;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

@Component
public class SalaryComponent {

    private static final BigDecimal salary_1 = BigDecimal.valueOf(1);

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private UserBasicMapper userBasicMapper;

    @Resource
    private SalaryFileMapper salaryFileMapper;

    @Resource
    private SalaryLevelMapper salaryLevelMapper;

    @Resource
    private SalaryManageMapper salaryManageMapper;

    @Resource
    private SalaryOneMapper salaryOneMapper;

    @Resource
    private SalaryTwoMapper salaryTwoMapper;

    @Resource
    private SalarySubsidyMapper salarySubsidyMapper;

    @Resource
    private SalaryHouseFundMapper salaryHouseFundMapper;

    @Resource
    private SalaryInsuranceMapper salaryInsuranceMapper;

    @Resource
    private SalaryRetireMapper salaryRetireMapper;

    /**
     * 注意调用该方法的外部方法需要开始事务
     * 创建薪资
     * @param salary 薪资信息
     *
     *
     * */
    public void createSalary(SalaryAllVO salary) {
        if (null == salary.getUserId()) {
            throw new BusinessException("未指定起薪人员", HttpStatus.BAD_REQUEST);
        }

        // 如果是退休需要校验退休薪资
        if (SalaryStatusEnum.ON_PAY.getCode() != salary.getSalaryStatus()) {
            throw new BusinessException("请先起薪再进行其他状态办理", HttpStatus.BAD_REQUEST);
        }

        checkSalary(salary);
        checkSalaryFile(salary.getSalaryFile());
        checkSalarySubsidy(salary.getSalarySubsidy());
        checkSalaryHouseFund(salary.getSalaryHouseFunds());
        checkSalaryInsurance(salary.getSalaryInsurance());

        // 非协议工资需要校验更多
        if (AgreeSalaryEnum.YES.getCode() != salary.getIsAgreeSalary()) {
            checkSalaryLevel(salary.getSalaryLevel());
            checkSalaryManage(salary.getSalaryManage());
            checkSalaryOne(salary.getSalaryOne());
            checkSalaryTwo(salary.getSalaryTwo());
        }

        Long userId = salary.getUserId();
        UserBasicPO userBasicPO = userBasicMapper.queryById(userId);
        if (null == userBasicPO) {
            throw new BusinessException("起薪人员不存在", HttpStatus.BAD_REQUEST);
        }

        // TODO 人员状态检查

        // 起薪状态检查
        SalaryPO salaryPO_old = salaryMapper.queryByUserId(userId);
        if (null != salaryPO_old) {
            throw new BusinessException("该人员已起薪", HttpStatus.BAD_REQUEST);
        }

        // 薪资主表
        SalaryPO salaryPO = new SalaryPO();
        salaryPO.setUserId(userId);
        salaryPO.setSalaryDeptId(salary.getSalaryDeptId());
        salaryPO.setStartSalaryData(salary.getStartSalaryData());
        salaryPO.setSalaryStatus(salary.getSalaryStatus());
        salaryPO.setIsAgreeSalary(salary.getIsAgreeSalary());
        salaryPO.setAgreeSalary(salary.getAgreeSalary());
        salaryPO.setIsTeachSeries(salary.getIsTeachSeries());
        salaryPO.setGovernmentStaff(salary.getGovernmentStaff());
        salaryMapper.insertSelective(salaryPO);

        // 档案工资
        SalaryFilePO salaryFilePO = new SalaryFilePO();
        BeanUtils.copyProperties(salary.getSalaryFile(), salaryFilePO);
        salaryFilePO.setId(null);
        salaryFilePO.setSalaryId(salaryPO.getId());
        salaryFileMapper.insertSelective(salaryFilePO);

        // 薪级工资
        SalaryLevelPO salaryLevelPO = new SalaryLevelPO();
        if (null != salary.getSalaryLevel()) {
            BeanUtils.copyProperties(salary.getSalaryLevel(), salaryLevelPO);
        }
        salaryLevelPO.setId(null);
        salaryLevelPO.setSalaryId(salaryPO.getId());
        salaryLevelMapper.insertSelective(salaryLevelPO);

        // 管理责任
        SalaryManagePO salaryManagePO = new SalaryManagePO();
        if (null != salary.getSalaryManage()) {
            BeanUtils.copyProperties(salary.getSalaryManage(), salaryManagePO);
        }
        salaryManagePO.setId(null);
        salaryManagePO.setSalaryId(salaryPO.getId());
        salaryManageMapper.insertSelective(salaryManagePO);

        // ONE
        SalaryOnePO salaryOnePO = new SalaryOnePO();
        if (null != salary.getSalaryOne()) {
            BeanUtils.copyProperties(salary.getSalaryOne(), salaryOnePO);
        }
        salaryOnePO.setId(null);
        salaryOnePO.setSalaryId(salaryPO.getId());
        salaryOneMapper.insertSelective(salaryOnePO);

        // TWO
        SalaryTwoPO salaryTwoPO = new SalaryTwoPO();
        if (null != salary.getSalaryTwo()) {
            BeanUtils.copyProperties(salary.getSalaryTwo(), salaryTwoPO);
        }
        salaryTwoPO.setId(null);
        salaryTwoPO.setSalaryId(salaryPO.getId());
        salaryTwoMapper.insertSelective(salaryTwoPO);

        // 补贴
        SalarySubsidyPO salarySubsidyPO = new SalarySubsidyPO();
        BeanUtils.copyProperties(salary.getSalarySubsidy(), salarySubsidyPO);
        salarySubsidyPO.setId(null);
        salarySubsidyPO.setSalaryId(salaryPO.getId());
        salarySubsidyMapper.insertSelective(salarySubsidyPO);

        // 公积金
        List<SalaryHouseFundPO> salaryHouseFundPOList = salary.getSalaryHouseFunds().stream().map(vo -> {
            SalaryHouseFundPO po = new SalaryHouseFundPO();
            BeanUtils.copyProperties(vo, po);
            po.setId(null);
            po.setSalaryId(salaryPO.getId());
            return po;
        }).toList();
        salaryHouseFundMapper.insertRecords(salaryHouseFundPOList);

        // 社保
        SalaryInsurancePO salaryInsurancePO = new SalaryInsurancePO();
        BeanUtils.copyProperties(salary.getSalaryInsurance(), salaryInsurancePO);
        salaryInsurancePO.setId(null);
        salaryInsurancePO.setSalaryId(salaryPO.getId());
        salaryInsuranceMapper.insertSelective(salaryInsurancePO);
    }

    /**
     * 注意调用该方法的外部方法需要开始事务
     * 更新薪资
     * @param salary 薪资信息
     *
     * */
    public void updateSalary(SalaryAllVO salary) {
        if (null == salary.getId()) {
            throw new BusinessException("未指定更新记录", HttpStatus.BAD_REQUEST);
        }

        // 检查主表
        checkSalary(salary);

        // 如果是停薪，则直接停薪，不做其他校验和更改
        if (SalaryStatusEnum.STOP.getCode() == salary.getSalaryStatus()) {
            stopSalary(salary);
            return;
        }

        checkSalaryFile(salary.getSalaryFile());
        checkSalarySubsidy(salary.getSalarySubsidy());
        checkSalaryHouseFund(salary.getSalaryHouseFunds());
        checkSalaryInsurance(salary.getSalaryInsurance());

        // 非协议工资需要校验更多
        if (AgreeSalaryEnum.YES.getCode() != salary.getIsAgreeSalary()) {
            checkSalaryLevel(salary.getSalaryLevel());
            checkSalaryManage(salary.getSalaryManage());
            checkSalaryOne(salary.getSalaryOne());
            checkSalaryTwo(salary.getSalaryTwo());
        }

        // 如果是退休需要校验退休薪资
        if (SalaryStatusEnum.RETIRE.getCode() == salary.getSalaryStatus()) {
            checkSalaryRetire(salary.getSalaryRetire());
        }

        //
        SalaryPO salaryPO = salaryMapper.queryById(salary.getId());
        if (null == salaryPO) {
            throw new BusinessException("未找到该薪资记录，请先针对人员起薪", HttpStatus.BAD_REQUEST);
        }

        // 薪资主表
        salaryPO.setSalaryDeptId(salary.getSalaryDeptId());
        salaryPO.setStartSalaryData(salary.getStartSalaryData());
        salaryPO.setSalaryStatus(salary.getSalaryStatus());
        salaryPO.setIsAgreeSalary(salary.getIsAgreeSalary());
        salaryPO.setAgreeSalary(salary.getAgreeSalary());
        salaryPO.setIsTeachSeries(salary.getIsTeachSeries());
        salaryPO.setGovernmentStaff(salary.getGovernmentStaff());
        salaryPO.setStopData(salary.getStopData());
        salaryPO.setStopType(salary.getStopType());
        salaryPO.setStopReason(salary.getStopReason());
        salaryPO.setRemark(salary.getRemark());
        salaryMapper.updateAllColumnById(salaryPO);

        // 档案工资
        salaryFileMapper.deleteBySalaryId(salaryPO.getId());
        SalaryFilePO salaryFilePO = new SalaryFilePO();
        BeanUtils.copyProperties(salary.getSalaryFile(), salaryFilePO);
        salaryFilePO.setId(null);
        salaryFilePO.setSalaryId(salaryPO.getId());
        salaryFileMapper.insertSelective(salaryFilePO);

        // 薪级工资
        salaryLevelMapper.deleteBySalaryId(salaryPO.getId());
        SalaryLevelPO salaryLevelPO = new SalaryLevelPO();
        if (null != salary.getSalaryLevel()) {
            BeanUtils.copyProperties(salary.getSalaryLevel(), salaryLevelPO);
        }
        salaryLevelPO.setId(null);
        salaryLevelPO.setSalaryId(salaryPO.getId());
        salaryLevelMapper.insertSelective(salaryLevelPO);

        // 管理责任
        salaryManageMapper.deleteBySalaryId(salaryPO.getId());
        SalaryManagePO salaryManagePO = new SalaryManagePO();
        if (null != salary.getSalaryManage()) {
            BeanUtils.copyProperties(salary.getSalaryManage(), salaryManagePO);
        }
        salaryManagePO.setId(null);
        salaryManagePO.setSalaryId(salaryPO.getId());
        salaryManageMapper.insertSelective(salaryManagePO);

        // ONE
        salaryOneMapper.deleteBySalaryId(salaryPO.getId());
        SalaryOnePO salaryOnePO = new SalaryOnePO();
        if (null != salary.getSalaryOne()) {
            BeanUtils.copyProperties(salary.getSalaryOne(), salaryOnePO);
        }
        salaryOnePO.setId(null);
        salaryOnePO.setSalaryId(salaryPO.getId());
        salaryOneMapper.insertSelective(salaryOnePO);

        // TWO
        salaryTwoMapper.deleteBySalaryId(salaryPO.getId());
        SalaryTwoPO salaryTwoPO = new SalaryTwoPO();
        if (null != salary.getSalaryTwo()) {
            BeanUtils.copyProperties(salary.getSalaryTwo(), salaryTwoPO);
        }
        salaryTwoPO.setId(null);
        salaryTwoPO.setSalaryId(salaryPO.getId());
        salaryTwoMapper.insertSelective(salaryTwoPO);

        // 补贴
        salarySubsidyMapper.deleteBySalaryId(salaryPO.getId());
        SalarySubsidyPO salarySubsidyPO = new SalarySubsidyPO();
        BeanUtils.copyProperties(salary.getSalarySubsidy(), salarySubsidyPO);
        salarySubsidyPO.setId(null);
        salarySubsidyPO.setSalaryId(salaryPO.getId());
        salarySubsidyMapper.insertSelective(salarySubsidyPO);

        // 公积金
        salaryHouseFundMapper.deleteBySalaryId(salaryPO.getId());
        List<SalaryHouseFundPO> salaryHouseFundPOList = salary.getSalaryHouseFunds().stream().map(vo -> {
            SalaryHouseFundPO po = new SalaryHouseFundPO();
            BeanUtils.copyProperties(vo, po);
            po.setId(null);
            po.setSalaryId(salaryPO.getId());
            return po;
        }).toList();
        salaryHouseFundMapper.insertRecords(salaryHouseFundPOList);

        // 社保
        salaryInsuranceMapper.deleteBySalaryId(salaryPO.getId());
        SalaryInsurancePO salaryInsurancePO = new SalaryInsurancePO();
        BeanUtils.copyProperties(salary.getSalaryInsurance(), salaryInsurancePO);
        salaryInsurancePO.setId(null);
        salaryInsurancePO.setSalaryId(salaryPO.getId());
        salaryInsuranceMapper.insertSelective(salaryInsurancePO);

        // 退休
        if (SalaryStatusEnum.RETIRE.getCode() == salary.getSalaryStatus()) {
            salaryRetireMapper.deleteBySalaryId(salaryPO.getId());
            SalaryRetirePO salaryRetirePO = new SalaryRetirePO();
            BeanUtils.copyProperties(salary.getSalaryRetire(), salaryRetirePO);
            salaryRetirePO.setId(null);
            salaryRetirePO.setSalaryId(salaryPO.getId());
            salaryRetireMapper.insertSelective(salaryRetirePO);
        }

    }

    /**
     * 检查薪资
     *
     * @param salaryVO
     * @return
     */
    private void checkSalary(SalaryVO salaryVO) {
        if (null == salaryVO.getSalaryDeptId()) {
            throw new BusinessException("未指定归属部门", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryVO.getStartSalaryData()) {
            throw new BusinessException("未指定起薪时间", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryVO.getSalaryStatus()) {
            throw new BusinessException("未指定状态", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryVO.getIsAgreeSalary()) {
            throw new BusinessException("未指定是否是协议薪资", HttpStatus.BAD_REQUEST);
        }
        if (AgreeSalaryEnum.YES.getCode() == salaryVO.getIsAgreeSalary()
                && (null == salaryVO.getAgreeSalary() || salaryVO.getAgreeSalary().compareTo(salary_1) < 0)) {
            throw new BusinessException("未指定协议薪资", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryVO.getIsTeachSeries()) {
            throw new BusinessException("未指定是否是教学系列", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryVO.getGovernmentStaff()) {
            throw new BusinessException("未指定是否是编制内", HttpStatus.BAD_REQUEST);
        }

        if (SalaryStatusEnum.STOP.getCode() == salaryVO.getSalaryStatus()) {
            if (null == salaryVO.getStopData()) {
                throw new BusinessException("未指定停薪时间", HttpStatus.BAD_REQUEST);
            }
            if (null == salaryVO.getStopType()) {
                throw new BusinessException("未指定停薪类型", HttpStatus.BAD_REQUEST);
            }
            if (!(SalaryStopTypeEnum.RESIGN.getCode() == salaryVO.getStopType()
                    || SalaryStopTypeEnum.RETIRE_DIE.getCode() == salaryVO.getStopType()
                    || SalaryStopTypeEnum.ON_PAY_DIE.getCode() == salaryVO.getStopType()
                    || SalaryStopTypeEnum.OTHER.getCode() == salaryVO.getStopType())) {
                throw new BusinessException("不支持的停薪类型", HttpStatus.BAD_REQUEST);
            }
        }
    }

    /**
     * 检查薪资
     *
     * @param salaryFile
     * @return
     */
    private void checkSalaryFile(SalaryFileVO salaryFile) {
        if (null == salaryFile) {
            throw new BusinessException("档案薪资必须指定", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getFilePostTypeId()) {
            throw new BusinessException("档案-未指定岗位类别", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getFilePostId()) {
            throw new BusinessException("档案-未指定岗位", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getPostDescribe()) {
            throw new BusinessException("档案-未指定岗位说明", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getFileLevelId()) {
            throw new BusinessException("档案-未指定薪级", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getLevelDescribe()) {
            throw new BusinessException("档案-未指定薪级说明", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getFilePostSalary() || salaryFile.getFilePostSalary().compareTo(salary_1) < 0) {
            throw new BusinessException("档案-未指定岗位工资", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryFile.getFileLevelSalary() || salaryFile.getFileLevelSalary().compareTo(salary_1) < 0) {
            throw new BusinessException("档案-未指定薪级工资", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 检查薪资
     *
     * @param salaryLevel
     * @return
     */
    private void checkSalaryLevel(SalaryLevelVO salaryLevel) {
        if (null == salaryLevel) {
            return;
        }
        if (null == salaryLevel.getPostTypeId()) {
            throw new BusinessException("薪级工资-未指定岗位类别", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryLevel.getLevelId()) {
            throw new BusinessException("薪级工资-未指定薪级", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryLevel.getLevelDescribe()) {
            throw new BusinessException("薪级工资-未指定薪级说明", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryLevel.getLevelSalary() || salaryLevel.getLevelSalary().compareTo(salary_1) < 0) {
            throw new BusinessException("薪级工资-未指定薪级工资", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 检查薪资
     *
     * @param salaryManage
     * @return
     */
    private void checkSalaryManage(SalaryManageVO salaryManage) {
        if (null == salaryManage) {
            return;
        }
        if (null == salaryManage.getManageAllowance()
                || salaryManage.getManageAllowance().compareTo(salary_1) < 0) {
            return;
        }

        if (null == salaryManage.getPostTypeId()) {
            throw new BusinessException("管理责任-未指定岗位类别", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryManage.getPostId()) {
            throw new BusinessException("管理责任-未指定岗位", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryManage.getPostDescribe()) {
            throw new BusinessException("管理责任-未指定岗位说明", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryManage.getManager()) {
            throw new BusinessException("管理责任-未指定管理责任", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 检查薪资
     *
     * @param salaryOne
     * @return
     */
    private void checkSalaryOne(SalaryOneVO salaryOne) {
        if (null == salaryOne) {
            return;
        }
        if (null == salaryOne.getPostTypeId()) {
            throw new BusinessException("基本工资-未指定岗位类别", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryOne.getPostId()) {
            throw new BusinessException("基本工资-未指定岗位", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryOne.getPostDescribe()) {
            throw new BusinessException("基本工资-未指定岗位说明", HttpStatus.BAD_REQUEST);
        }
//        if (null == salaryOne.getPostSalary() ||  salaryOne.getPostSalary().compareTo(salary_1) < 0) {
//            throw new BusinessException("基本工资-未指定岗位工资", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryOne.getPerformanceAward() ||  salaryOne.getPerformanceAward().compareTo(salary_1) < 0) {
//            throw new BusinessException("基本工资-未指定基础绩效奖", HttpStatus.BAD_REQUEST);
//        }
    }

    /**
     * 检查薪资
     *
     * @param salaryTwo
     * @return
     */
    private void checkSalaryTwo(SalaryTwoVO salaryTwo) {
        if (null == salaryTwo) {
            return;
        }
        if (null == salaryTwo.getPostTypeId()) {
            throw new BusinessException("绩效工资-未指定岗位类别", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryTwo.getPostId()) {
            throw new BusinessException("绩效工资-未指定岗位", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryTwo.getPostDescribe()) {
            throw new BusinessException("绩效工资-未指定岗位说明", HttpStatus.BAD_REQUEST);
        }
//        if (null == salaryTwo.getBasicAllowance()) {
//            throw new BusinessException("绩效工资-未指定基础津贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryTwo.getPostAllowance()) {
//            throw new BusinessException("绩效工资-未指定岗位津贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryTwo.getAchievementAward()) {
//            throw new BusinessException("绩效工资-未指定基本业绩奖励", HttpStatus.BAD_REQUEST);
//        }
    }

    /**
     * 检查薪资
     *
     * @param salarySubsidy
     * @return
     */
    private void checkSalarySubsidy(SalarySubsidyVO salarySubsidy) {
        if (null == salarySubsidy) {
            throw new BusinessException("补贴信息必须指定", HttpStatus.BAD_REQUEST);
        }
//        if (null == salarySubsidy.getPostSubsidy()) {
//            throw new BusinessException("补贴-未指定岗位补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getPropertySubsidy()) {
//            throw new BusinessException("补贴-未指定物业补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getTrafficSubsidy()) {
//            throw new BusinessException("补贴-未指定交通补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getPhoneSubsidy()) {
//            throw new BusinessException("补贴-未指定通讯补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getSecrecySubsidy()) {
//            throw new BusinessException("补贴-未指定保密补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getHandleSubsidy()) {
//            throw new BusinessException("补贴-未指定办案补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getDoctorAllowance()) {
//            throw new BusinessException("补贴-未指定博士津贴", HttpStatus.BAD_REQUEST);
//        }
        if (null != salarySubsidy.getDoctorAllowance()
                && salarySubsidy.getDoctorAllowance().compareTo(salary_1) > 0
                && null == salarySubsidy.getDoctorAllowanceStart()) {
            throw new BusinessException("补贴-未指定博士津贴开始时间", HttpStatus.BAD_REQUEST);
        }
//        if (null == salarySubsidy.getInstructorAllowance()) {
//            throw new BusinessException("补贴-未指定辅导员津贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getPoliticalAllowance()) {
//            throw new BusinessException("补贴-未指定思政津贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getCommutingSubsidy()) {
//            throw new BusinessException("补贴-未指定通勤补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getOnlyChildSubsidy()) {
//            throw new BusinessException("补贴-未指定独生子补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getRentalSubsidy()) {
//            throw new BusinessException("补贴-未指定租房补贴", HttpStatus.BAD_REQUEST);
//        }
        if (null != salarySubsidy.getRentalSubsidy()
                && salarySubsidy.getRentalSubsidy().compareTo(salary_1) > 0
                && null == salarySubsidy.getRentalSubsidyDate()) {
            throw new BusinessException("补贴-未指定租房补贴开始时间", HttpStatus.BAD_REQUEST);
        }
//        if (null == salarySubsidy.getSalary10()) {
//            throw new BusinessException("补贴-未指定工资百分之10", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getNursingAge()) {
//            throw new BusinessException("补贴-未指定教护龄", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getMissMealsSubsidy()) {
//            throw new BusinessException("补贴-未指定误餐补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getDriverAllowance()) {
//            throw new BusinessException("补贴-未指定驾驶员补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getVeteranCadreSubsidy()) {
//            throw new BusinessException("补贴-未指定老干部补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getFamilyPlanningSubsidy()) {
//            throw new BusinessException("补贴-未指定计生补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getCountrySubsidy()) {
//            throw new BusinessException("补贴-未指定国贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getProvinceJobSubsidy()) {
//            throw new BusinessException("补贴-未指定省定职补贴", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salarySubsidy.getCityJobSubsidy()) {
//            throw new BusinessException("补贴-未指定市定职补贴", HttpStatus.BAD_REQUEST);
//        }
    }

    /**
     * 检查薪资
     *
     * @param salaryHouseFunds
     * @return
     */
    private void checkSalaryHouseFund(List<SalaryHouseFundVO> salaryHouseFunds) {
        if (CollectionUtils.isEmpty(salaryHouseFunds)) {
            throw new BusinessException("公积金-至少添加当年公积金", HttpStatus.BAD_REQUEST);
        }
        int count = 0;
        for (SalaryHouseFundVO salaryHouseFundVO : salaryHouseFunds) {
//            if (null == salaryHouseFundVO.getFundCardinal()) {
//                throw new BusinessException("公积金-未指定基数", HttpStatus.BAD_REQUEST);
//            }
//            if (null == salaryHouseFundVO.getFundStandard()) {
//                throw new BusinessException("公积金-未指定标准", HttpStatus.BAD_REQUEST);
//            }
            if (null == salaryHouseFundVO.getBelongYear()) {
                throw new BusinessException("公积金-未指定所属年", HttpStatus.BAD_REQUEST);
            }
            if (YesNoConstant.YES == salaryHouseFundVO.getCurrentYear()) {
                count++;
            }
        }
        if (count != 1) {
            throw new BusinessException("公积金-有且仅有一条当前年公积金", HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 检查薪资
     *
     * @param salaryInsurance
     * @return
     */
    private void checkSalaryInsurance(SalaryInsuranceVO salaryInsurance) {
        if (null == salaryInsurance) {
            throw new BusinessException("社保必须指定", HttpStatus.BAD_REQUEST);
        }
//        if (null == salaryInsurance.getSocialCardinal()) {
//            throw new BusinessException("社保-未指定社保基数", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getMedicalCardinal()) {
//            throw new BusinessException("社保-未指定医保基数", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getEndowmentCardinal()) {
//            throw new BusinessException("社保-未指定养老基数", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getMedicalInsurance()) {
//            throw new BusinessException("社保-未指定医疗保险", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getCareerInsurance()) {
//            throw new BusinessException("社保-未指定失业保险", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getEndowmentInsurance()) {
//            throw new BusinessException("社保-未指定养老保险", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getOccupationAnnuity()) {
//            throw new BusinessException("社保-未指定职业年金", HttpStatus.BAD_REQUEST);
//        }
//        if (null == salaryInsurance.getUnionDues()) {
//            throw new BusinessException("社保-未指定工会会费", HttpStatus.BAD_REQUEST);
//        }
    }

    /**
     * 检查薪资
     *
     * @param salaryRetire
     * @return
     */
    private void checkSalaryRetire(SalaryRetireVO salaryRetire) {
        if (null == salaryRetire) {
            throw new BusinessException("退休人员必须指定退休待遇", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getRetireType()) {
            throw new BusinessException("退休-未指定退休类型", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getRetireTo()) {
            throw new BusinessException("退休-未指定转入部门", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getRetireTime()) {
            throw new BusinessException("退休-未指定退休时间", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getRetireDealTime()) {
            throw new BusinessException("退休-未指定退休办理", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getSchoolSubsidy()) {
            throw new BusinessException("退休-未指定校内补贴", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getPropertySubsidy()) {
            throw new BusinessException("退休-未指定物业补贴", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getTrafficSubsidy()) {
            throw new BusinessException("退休-未指定交通补贴", HttpStatus.BAD_REQUEST);
        }
        if (null == salaryRetire.getPhoneSubsidy()) {
            throw new BusinessException("退休-未指定通讯补贴", HttpStatus.BAD_REQUEST);
        }
    }

    private void stopSalary(SalaryAllVO updateSalaryReq) {
        SalaryPO salaryPO = salaryMapper.queryById(updateSalaryReq.getId());
        if (null == salaryPO) {
            throw new BusinessException("未找到该薪资记录，请先针对人员起薪", HttpStatus.BAD_REQUEST);
        }
        salaryPO.setStartSalaryData(updateSalaryReq.getStartSalaryData());
        salaryPO.setSalaryStatus(SalaryStatusEnum.STOP.getCode());
        salaryPO.setIsAgreeSalary(updateSalaryReq.getIsAgreeSalary());
        salaryPO.setAgreeSalary(updateSalaryReq.getAgreeSalary());
        salaryPO.setIsTeachSeries(updateSalaryReq.getIsTeachSeries());
        salaryPO.setGovernmentStaff(updateSalaryReq.getGovernmentStaff());
        salaryPO.setStopData(updateSalaryReq.getStopData());
        salaryPO.setStopType(updateSalaryReq.getStopType());
        salaryPO.setStopReason(updateSalaryReq.getStopReason());
        salaryPO.setRemark(updateSalaryReq.getRemark());
        salaryMapper.updateAllColumnById(salaryPO);
    }
}
